﻿using AutoFlatnessTest.Db;
using AutoFlatnessTest.Plc;
using AutoFlatnessTest.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoFlatnessTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : HandyControl.Controls.Window
    {
        public static MainWindowVModel VModel = new MainWindowVModel();

        /// <summary>
        /// 选中的项编号 0无 1首页 10数据（11综合 12详情） 50设置(51综合信息 52项目设计 53标定PLC)
        /// </summary>
        public static int SelectItemNum
        {
            get
            {
                if (tabIndex1 == 1)
                    return tabIndex1;
                else if (tabIndex1 == 10)
                    return tabIndex2;
                else if (tabIndex1 == 50)
                    return tabIndex5;

                return 0;
            }
        }
        private static int tabIndex1 = 0;
        private static int tabIndex2 = 0;
        private static int tabIndex5 = 0;
        public static TaskTime taskTime = new TaskTime();

        public MainWindow()
        {
            var mainStartWindow = new MainStartWindow();
            mainStartWindow.ShowDialog();
            if (mainStartWindow.isUserClose)
            {
                this.Close();
            }

            MainRun();

            InitializeComponent();

            this.DataContext = VModel;
            tab2.DataContext = home.VModel;
            tab5.DataContext = home.VModel;
        }

        private void MainRun()
        {
            taskTime.AddForTask(() =>
            {

                VModel.SetTbIn();
                var err = TestRecordDb.UpData();
                if (string.IsNullOrEmpty(err))
                    VModel.SetTbOk();
                else
                    VModel.SetTbErr(err);

            }, TimeSpan.FromHours(1));
        }

        private void load(object sender, RoutedEventArgs e)
        {
            VModel.RunTime();
            RunPlcErr();
            taskTime.Start();
        }

        private void RunPlcErr()
        {
            _ = Task.Run(() =>
            {
                VModel.Info = "正在连接设备";
                while (true)
                {
                    try
                    {
                        if (InovanceRun.Plc == null)
                        {
                            VModel.Info = "无法连接设备";
                            VModel.IsJinGao = true;
                        }
                        else
                        {
                            InovanceRun.IsConnect();
                            VModel.Info = "已连接设备";
                            VModel.IsJinGao = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        VModel.Info = "无法连接设备";
                    }
                    finally
                    {
                        ////弹窗打开，更新信息
                        //UpdataInfo();
                    }

                    Thread.Sleep(1000);
                }
            });
        }

        private void But_Close(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void But_gy(object sender, RoutedEventArgs e)
        {
            WinWith winWith = new WinWith();
            winWith.ShowDialog();
        }

        private void selectTab1(object sender, SelectionChangedEventArgs e)
        {
            if (!(sender is TabControl tabControl))
                return;

            var sItem = tabControl.SelectedItem;
            if (sItem == null)
                tabIndex1 = 0;

            if (sItem == tab1)
                tabIndex1 = 1;
            if (sItem == tab2)
                tabIndex1 = 10;
            else if (sItem == tab5)
                tabIndex1 = 50;
        }

        private void selectTab10(object sender, SelectionChangedEventArgs e)
        {
            if (!(sender is TabControl tabControl))
                return;

            var sItem = tabControl.SelectedItem;
            if (sItem == null)
                tabIndex2 = 0;
            if (sItem == tabSet11)
                tabIndex2 = 11;
            else if (sItem == tabSet12)
                tabIndex2 = 12;
        }

        private void selectTab50(object sender, SelectionChangedEventArgs e)
        {
            if (!(sender is TabControl tabControl))
                return;

            var sItem = tabControl.SelectedItem;
            if (sItem == null)
                tabIndex5 = 0;

            if (sItem == tabSet51)
                tabIndex5 = 51;
            else if (sItem == tabSet52)
                tabIndex5 = 52;
            else if (sItem == tabSet53)
                tabIndex5 = 53;
        }


        private void mouseErr2(object sender, RoutedEventArgs e)
        {

        }


        private void mouseErr(object sender, MouseButtonEventArgs e)
        {

        }

        private void close(object sender, EventArgs e)
        {
            try
            {
                InovanceRun.Plc?.CloseConnect();
                JiEnShiRun.DeviceSmi?.Close();
                JiEnShiRun.DeviceSma?.Close();
            }
            catch (Exception ex)
            {

            }
        }

        private void But_Min(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void But_Restore(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
        }

        private void dowmwin(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void upwin(object sender, MouseButtonEventArgs e)
        {

        }

        private void movewin(object sender, MouseEventArgs e)
        {

        }

        private void mouseTbErr(object sender, MouseButtonEventArgs e)
        {
            if (!string.IsNullOrEmpty(VModel.TbErr))
            {
                MessageBox.Show(VModel.TbErr, "同步数据失败", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
